import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np

def create_plot():
    # 生成示例数据
    x = np.arange(0, 10, 0.1)
    y = np.sin(x)

    # 创建图形和子图
    fig, ax = plt.subplots()
    ax.plot(x, y)
    ax.set_title("折线图示例")
    ax.set_xlabel("X 轴")
    ax.set_ylabel("Y 轴")

    # 将 matplotlib 图形嵌入到 Tkinter Canvas 中
    canvas = FigureCanvasTkAgg(fig, master=root)
    canvas.draw()
    canvas.get_tk_widget().pack()

# 创建主窗口
root = tk.Tk()
root.title("Tkinter 数据可视化示例")

# 创建一个按钮，点击后显示图表
button = tk.Button(root, text="显示图表", command=create_plot)
button.pack()

root.mainloop()